<template>
  <div class="spec-preview">
    <!-- 因为skuImageList很有可能是undefined，所以再取里面的值是就会报错，所以要采用可选链,如果是undefined，就不继续找了 -->
    <img :src="skuImageList[defaultIndex]?.imgUrl" />
    <div class="event" @mousemove="maskMove"></div>
    <div class="big">
      <img :src="skuImageList[defaultIndex]?.imgUrl"  ref="bigImgEle"/>
    </div>
    <div class="mask" ref="maskEle"></div>
  </div>
</template>

<script>
  export default {
    name: "Zoom",
    data(){
        return{
            defaultIndex:0
        }
    },
    mounted() {
    // 将缩略图传过来的图片下标，修改放大镜的下标
      this.$bus.$on("getZoomIndex",(index)=>{
            this.defaultIndex = index
      })
    },
    methods:{
    //1.计算蒙版的位置
      maskMove(e){
          //鼠标移动的距离
          const mouseMoveDistance ={
              left:e.offsetX,
              top:e.offsetY
          }
          //蒙版的位置
          const maskLoaction ={
              left:mouseMoveDistance.left - this.$refs.maskEle.offsetWidth/2,
              top:mouseMoveDistance.top-this.$refs.maskEle.offsetHeight/2
          }
          //处理边界
          if(maskLoaction.left<0){
               maskLoaction.left =0
          }else if(maskLoaction.left>e.target.clientWidth-this.$refs.maskEle.offsetWidth){
              maskLoaction.left=e.target.clientWidth-this.$refs.maskEle.offsetWidth
          }
          if(maskLoaction.top<0){
               maskLoaction.top =0
          }else if(maskLoaction.top>e.target.clientHeight-this.$refs.maskEle.offsetHeight){
              maskLoaction.top=e.target.clientHeight-this.$refs.maskEle.offsetHeight
          }
          //赋值给蒙版
          this.$refs.maskEle.style.left = maskLoaction.left + 'px'
          this.$refs.maskEle.style.top = maskLoaction.top + 'px'
          //大图移动的距离是蒙版的两倍,大图移动方向和蒙版相反
          this.$refs.bigImgEle.style.left = -2 * maskLoaction.left +'px'
          this.$refs.bigImgEle.style.top = -2 * maskLoaction.top +'px'
      }
    },
    //获取父组件传过来的图片，定义一个初始下标，让放大镜的大图和小图都先默认展示数组中的第一张图片
    props:["skuImageList"]
  }
</script>

<style lang="less">
  .spec-preview {
    position: relative;
    width: 400px;
    height: 400px;
    border: 1px solid #ccc;

    img {
      width: 100%;
      height: 100%;
    }

    .event {
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
      z-index: 998;
    }

    .mask {
      width: 50%;
      height: 50%;
      background-color: rgba(0, 255, 0, 0.3);
      position: absolute;
      left: 0;
      top: 0;
      display: none;
    }

    .big {
      width: 100%;
      height: 100%;
      position: absolute;
      top: -1px;
      left: 100%;
      border: 1px solid #aaa;
      overflow: hidden;
      z-index: 998;
      display: none;
      background: white;

      img {
        width: 200%;
        max-width: 200%;
        height: 200%;
        position: absolute;
        left: 0;
        top: 0;
      }
    }

    .event:hover~.mask,
    .event:hover~.big {
      display: block;
    }
  }
</style>